package com.gmail.shazi199.gauth;

import java.security.GeneralSecurityException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base32;

public class GAuth {
	public String computePin(String secret, Long counter) {
		if (secret == null || secret.length() == 0) {
			return "Null or empty secret";
		}
		try {
			final byte[] keyBytes = new Base32().decode(secret.toUpperCase());
			Mac mac = Mac.getInstance("HMACSHA1");
			mac.init(new SecretKeySpec(keyBytes, ""));
			PasscodeGenerator pcg = new PasscodeGenerator(mac);
			return pcg.generateTimeoutCode();
		} catch (GeneralSecurityException e) {
			return "General security exception";
		}
	}
}
